package com.cwj.controller;

import com.cwj.controller.utils.Code;
import com.cwj.controller.utils.Result;
import com.cwj.pojo.*;
import com.cwj.service.NetworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author ACER
 */
@RestController
@RequestMapping("/network")
public class NetworkController {

    @Autowired
    private NetworkService networkService;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        Integer code = networkService.login(user) ? Code.GET_OK : Code.GET_ERR;
        String msg = networkService.login(user) ? "登录成功" : "登录失败";
        return new Result(code,null,msg);
    }

    @GetMapping("/getMessage")
    public Result getMessage(){
        return new Result(Code.GET_OK,networkService.getMessage(),"");
    }

    @PostMapping("/saveMessage")
    public Result saveMessage(@RequestBody Message message){
        networkService.saveMessage(message.getMessage());
        return new Result(Code.SAVE_OK,null,"");
    }

    @GetMapping("/userLogin/{account}/{password}")
    public Result userLogin(@PathVariable String account, @PathVariable String password, HttpSession session){
        User user = networkService.getUser(account,password);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? "登录成功" : "登录失败！！！ 账号或密码错误";
        if (user != null){
            session.setAttribute("identity", user);
        }
        return new Result(code,user,msg);
    }

    @GetMapping("/adminLogin/{account}/{password}")
    public Result adminLogin(@PathVariable String account, @PathVariable String password, HttpSession session){
        Admin admin = networkService.getAdmin(account,password);
        Integer code = admin != null ? Code.GET_OK : Code.GET_ERR;
        String msg = admin != null ? "登录成功" : "登录失败！！！ 账号或密码错误";
        if (admin != null){
            session.setAttribute("identity", admin);
        }
        return new Result(code,admin,msg);
    }

    /**
     * 查找登录信息
     * @param session
     * @return
     */
    @GetMapping("/session")
    public Result getSessionAdmin(HttpSession session){
        Object identity = (Object) session.getAttribute("identity");
        Integer code = identity != null ? Code.GET_OK : Code.GET_ERR;
        String msg = identity != null ? "用户存在" : "用户不存在";
        return new Result(code,identity,msg);
    }

    @GetMapping("/CSRF/getSession")
    public Result getUser(){
        Users users = networkService.getUsers(1);
        return new Result(Code.GET_OK,users,"用户存在");
    }

    @GetMapping("/CSRF/updata/{account}/{password}")
    public Result updata(@PathVariable String account, @PathVariable String password){
        Users users = new Users();
        users.setUid(1);
        users.setAccount(account);
        users.setPassword(password);
        networkService.updataUsers(users);
        return new Result(Code.UPDATE_OK,null,"用户信息更改成功!!");
    }

    @PostMapping("XSS")
    public Result XSS(@RequestBody Message message){
        return new Result(Code.GET_OK,message,null);
    }

    @GetMapping("/visit/{username}/{password}")
    public Result visitLogin(@PathVariable String username, @PathVariable String password, HttpSession session){
        User03 user = networkService.user03Login(username,password);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? "登录成功" : "登录失败！！！ 账号或密码错误";
        if (user != null){
            session.setAttribute("user", user);
        }
        return new Result(code,user,msg);
    }

    @GetMapping("/visit/getSession")
    public Result visitGetSession(HttpSession session){
        User03 user = (User03) session.getAttribute("user");
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? "用户存在" : "用户不存在";
        return new Result(code,user,msg);
    }

    @GetMapping("/visit/seeUser/{username}")
    public Result seeUser(@PathVariable String username){
        User03 user = networkService.getUser03(username);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? "用户存在" : "用户不存在";
        return new Result(code,user,msg);
    }

    @PostMapping("/numSQL")
    public Result numSQL(@RequestBody User02 user){
        System.out.println(user.getUid());
        return new Result(Code.GET_OK,networkService.numSQL(user.getUid()),null);
    }

    @PostMapping("/strSQL")
    public Result strSQL(@RequestBody User02 user){
        System.out.println(user.getUid());
        return new Result(Code.GET_OK,networkService.strSQL(user.getUid()),null);
    }

}
